# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : fCore.py
# Date      : 2019/8/23 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import maya.cmds as mc
import maya.mel as mel

import configs.constants.facial_constant as fc
import configs.constants.global_constant as gc
import configs.constants.suffix_constant as sc
import configs.constants.nodeType_constant as ntc
import configs.constants.attribute_constant as ac

reload(fc)
reload(gc)
reload(sc)
reload(ntc)
reload(ac)

import scripts.commonCmds as con
reload(con)
import scripts.curveCmds as cr
reload(cr)

class FacialCore(object):
    def __init__(self,tag=""):
        super(FacialCore,self).__init__()
        self._tag = tag
        
        self.toParent = con.lname(fc.facial,fc.toParent)
        self.static = con.lname(fc.facial,fc.static)
        self.doDelete = con.lname(fc.facial,fc.deDelete)
        self.facial_sets = con.lname(fc.facial,fc.sets)
        self.blsTgtgGrp = con.lname(fc.facial,"blendShape_tgt")
        
        self.local_toParent = con.lname(fc.facial,tag,fc.toParent)
        self.local_static = con.lname(fc.facial,tag,fc.static)
        self.local_doDelete = con.lname(fc.facial,tag,fc.deDelete)
        self.local_sets = con.lname(fc.facial,tag,fc.sets)
        self.local_blsTgtgGrp = con.lname(fc.facial,tag,"blendShape_tgt")
        
        

    def transform_init(self):

        if not mc.objExists(self.toParent):
            mc.createNode(ntc.tTransform,name =  self.toParent, ss=True)
        if not mc.objExists(self.static):
            mc.createNode(ntc.tTransform, name=self.static, ss=True)
        if not mc.objExists(self.doDelete):
            mc.createNode(ntc.tTransform, name=self.doDelete, ss=True)
        
        if not mc.objExists(self.local_toParent):
            mc.createNode(ntc.tTransform,name = self.local_toParent, ss=True,parent=self.toParent)
        if not mc.objExists(self.local_static):
            mc.createNode(ntc.tTransform, name=self.local_static, ss=True,parent=self.static)
        if not mc.objExists(self.local_doDelete):
            mc.createNode(ntc.tTransform, name=self.local_doDelete, ss=True,parent=self.doDelete)
        self.sets_init()
        
        # --- blendShape target
        if not mc.objExists(self.blsTgtgGrp):
            mc.createNode(ntc.tTransform,name = self.blsTgtgGrp, ss=True)
        if not mc.objExists(self.local_blsTgtgGrp):
            mc.createNode(ntc.tTransform,name = self.local_blsTgtgGrp, ss=True,parent=self.blsTgtgGrp)
            
        return
    
    
    def sets_init(self):
        if not mc.objExists(self.facial_sets):
            mc.sets(em=True,name=self.facial_sets)
        if not mc.objExists(self.local_sets):
            mc.sets(em=True,name=self.local_sets,)
            mc.sets(self.local_sets, include=self.facial_sets)
        return
    
    def local_ctrl(self,obj,name,shape="circle", colorId=0,scale=1,mirror=False):
        
        ctr = cr.objToControl(obj,name,shape, colorId)
        mc.setAttr(ctr+".s",scale,scale,scale)
        #
        # if mirror:
        #     con.localMirror(ctr,space="world")
        #
        return ctr
    
    def preference(self):
        self.transform_init()
        headGeo = "Head_fgeo"
        if mc.objExists(headGeo) and not mc.objExists(con.lname(fc.facial,sc.sJot)):
            clu = mc.cluster(headGeo)[1]
            x, y, z = mc.xform(clu, q=True, rp=True)
            _jot = mc.createNode("joint",name = con.lname(fc.facial,sc.sJot),ss=True)
            mc.setAttr(_jot+".t",x,y,z)
            con.lockHideAttr(_jot)
            mc.delete(clu)
            mc.parent(_jot,self.local_static)
            
        org = mc.rename(mc.duplicate(headGeo),con.lname(headGeo,"org"))
        mc.parent(org,self.blsTgtgGrp)
        sgeoGrp = con.lname(fc.facial,sc.sGeo,sc.sGrp)
        if not mc.objExists(sgeoGrp):
            mc.createNode(ntc.tTransform,name=sgeoGrp)
        sgeo = con.lname(fc.facial,"f"+sc.sGeo)
        if not mc.objExists(sgeo):
            mc.createNode(ntc.tTransform,name=sgeo,parent=sgeoGrp)
        sgeoSS = con.lname(fc.facial,"f"+sc.sGeo+"SS")
        if not mc.objExists(sgeoSS):
            mc.createNode(ntc.tTransform,name=sgeoSS,parent=sgeoGrp)
            
        for tl in fc.taglist:
            nn = con.lname(tl,"f"+sc.sGeo)
            if mc.objExists(nn):
                nnpat = mc.listRelatives(nn,p=True)
                if nnpat and nnpat[0] != sgeo:
                    mc.parent(nn,sgeo)
                nss = con.lname(nn+"SS")
                if not mc.objExists(nss):
                    nss = mc.duplicate(nn,name=nss)[0]
                    mc.parent(nss,sgeoSS)
                    bls = mc.blendShape(nn,nss,name = con.lname(tl,"toSS",sc.sBls),w=[0,1])
                    mc.skinCluster(_jot,nn,name = con.lname(nn,ntc.tSkinCluster))
                    mc.skinCluster(_jot,nss,name = con.lname(nss,ntc.tSkinCluster))
        
        
        return
    def hide_doDelete(self):
        mc.hide(con.lname(fc.facial, "tmp", self._tag, fc.deDelete))
        mc.select(cl=True)
        return
    
    def other_ctr_loc(self,name):
        sel = mc.ls(sl=True)
        clu = mc.cluster(sel)[1]
        x, y, z = mc.xform(clu, q=True, rp=True)
        ctr_list=self.ctr_loc(clu, name)
        mc.delete(clu)
        
        for ctr in ctr_list:
            mc.parent(ctr+"_Zero",self.local_toParent)
            # print ctr.replace(sc.sCtr,sc.sJot) + "_Zero"
            mc.parent(ctr.replace(sc.sCtr,sc.sJot) + "_Zero", self.local_toParent)
            con.lockHideAttr(ctr,["v"])
        return
    def ctr_loc(self,obj,name,):
        x, y, z = mc.xform(obj, q=True, rp=True)
        ctr_list = list()
        for lr in [fc.lf,fc.rt]:
            
            _nn = con.lname(lr, name)
            ctr = cr.createControl(con.lname(_nn,sc.sCtr), "cube", colorId=18)
            mc.setAttr(ctr+".t",x, y, z)
            # cr.scaleXYZ(ctr, 0.05)
            jot = mc.createNode(ntc.tJoint, name=con.lname(_nn, sc.sJot), parent=ctr)
            # mc.setAttr(jot + ".radius", 0.035)
            if lr == fc.rt:
                con.localMirror(ctr,"world")
            ctr_grp = con.emptyGroup(ctr)
            jot_grp = con.emptyGroup(jot,["Zero","Con","Sdk","Ci"])
            for _ctr, _jot in zip(ctr_grp[2:] + [ctr], jot_grp[2:]):
                print _ctr, _jot
                for _at in ["t", "r", "s"]:
                    mc.connectAttr(con.lAttr(_ctr, _at), con.lAttr(_jot, _at))
            ctr_list.append(ctr)
        
        return ctr_list
    
if __name__ == '__main__':
    pass